import { defineConfig, loadEnv, ConfigEnv, UserConfig } from 'vite'
import { initVitePlugins } from './vite/plugins'
import { initProxy } from './vite/plugins/proxy'
import { wrapperEnv } from './vite/getEnv'
import { resolve } from 'path'
const pathSrc = resolve(__dirname, 'src')
// https://vitejs.dev/config/
export default defineConfig(({ mode, command }: ConfigEnv): UserConfig => {
  const root = process.cwd()
  const env = loadEnv(mode, root)
  const viteEnv = wrapperEnv(env)
  return {
    server: {
      host: '0.0.0.0',
      port: viteEnv.VITE_PORT,
      open: viteEnv.VITE_OPEN,
      proxy: initProxy(viteEnv.VITE_PROXY)
    },
    resolve: {
      alias: {
        '@': pathSrc
      }
    },
    plugins: initVitePlugins(viteEnv, command === 'build'),
    build: {
      outDir: 'D:/nginx-1.25/html',
      minify: 'esbuild',
      sourcemap: false
    },
    css: {
      // CSS 预处理器
      preprocessorOptions: {
        // 定义全局 SCSS 变量
        scss: {
          silenceDeprecations: ['legacy-js-api'],
          javascriptEnabled: true,
          additionalData: `@import "@/assets/styles/variables.scss";`
        }
      }
    },
    optimizeDeps: {
      include: [
        'vue',
        'vue-router',
        'pinia',
        'vue-i18n',
        'axios',
        '@vueuse/core',
        '@opentiny/vue',
        'pinia-plugin-persistedstate',
        'js-cookie',
        'nprogress'
      ]
    }
  }
})
